package com.ruoyi.consumer.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.consumer.domain.WebsocketObj;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

public class WebsocketObjEncoder implements Encoder.Text<WebsocketObj> {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String encode(WebsocketObj object) throws EncodeException {
        try {
            // 使用 Jackson 序列化将对象转换为 JSON 字符串
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            throw new EncodeException(object, "Failed to encode WebsocketObj", e);
        }
    }

    @Override
    public void init(EndpointConfig config) {
        // 初始化编码器
    }

    @Override
    public void destroy() {
        // 销毁编码器
    }
}
